package com.jwiki.app.jkeeper.controller.form;

import java.util.Arrays;

import org.apache.commons.lang3.ArrayUtils;

public class PageForm<T> extends BaseForm<T> {

    private Integer offset;

    private Integer limit;

    private String[] orderBy;

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String[] getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String[] orderBy) {
        if (ArrayUtils.isNotEmpty(orderBy)) {
            this.orderBy = Arrays.stream(orderBy).map(it -> {
                if (it.startsWith("+")) {
                    return it.substring(1) + " ASC";
                } else if (it.startsWith("-")) {
                    return it.substring(1) + " DESC";
                } else {
                    return it;
                }
            }).toArray(String[]::new);
        }
    }

}
